package com.hoow.shakoow.shake.components.imageView

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.bumptech.glide.request.transition.Transition
import com.hoow.shakoow.R
import com.hoow.shakoow.extend.isValidLink

/** 结果页展示view pager 适配器 */
class ShakeImageGallaryAdapter(context: Context, arrayList: List<String>) :
    PagerAdapter() {
    private val mContext: Context
    private val dataList: List<String>

    private lateinit var imageAiImageGallary: ImageView

    //结果图的bitmap
    private var mResultBitmap: Bitmap? = null

    init {
        mContext = context
        dataList = arrayList
    }

    /**
     * 点击回调
     *
     * @return Int 位置
     * @return RevObjAiHomeDemo 值
     * */
    private var mOnClickActionListener: (Int, String, Bitmap?, Int) -> Unit =
        { action, obj, bitmap, position -> }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        //子View显示
        val view: View =
            View.inflate(mContext, R.layout.fragment_shake_image_gallary, null)

        imageAiImageGallary =
            view.findViewById(R.id.imageAiImageGallary)

        dataList.let {
            //当前obj
            val data = it[position]

            //初始化图片显示
            initImageUI(data)

            //点击图片事件
            imageAiImageGallary.setOnClickListener {
                mOnClickActionListener.invoke(
                    2,
                    data,
                    null,
                    position
                )
            }
        }

        //添加到父控件
        container.addView(view)
        return view
    }

    override fun getCount(): Int {
        return dataList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        //从viewpager中移除掉
        container.removeView(obj as View)
    }

    /**
     * 设置点击回调
     */
    fun setItemClickListener(listener: (Int, String, Bitmap?, Int) -> Unit) {
        mOnClickActionListener = listener
    }

    /** 获取bitmap */
    fun getCurrentBitmap(): Bitmap? {
        return mResultBitmap
    }

    /** 初始化image显示 */
    private fun initImageUI(result: String) {
        imageAiImageGallary.visibility = View.VISIBLE

        //如果是合法链接，加载图片
        val imgUrl = result.orEmpty()
        if (imgUrl.isValidLink()) {
            getGlideCacheImage(result)
        }
    }

    /** 获取glide图片 */
    private fun getGlideCacheImage(data: String?) {
        //如果是合法链接，加载图片
        val imgUrl = data.orEmpty()
        if (imgUrl.isValidLink()) {
            val requestOptions = RequestOptions().clone().optionalFitCenter()
            val target =
                object :
                    BitmapImageViewTarget(imageAiImageGallary) {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        super.onResourceReady(resource, transition)
                        imageAiImageGallary.setImageBitmap(resource)
                        mResultBitmap = resource
                        println("获取glide图片")
                    }
                }

            Glide.with(mContext)
                .asBitmap()
                .load(imgUrl)
                .apply(requestOptions)
                .into(target)
        }
    }
}